//
//  newsImageListController.m
//  MtimeMovie
//
//  Created by lllyyy on 15/7/22.
//  Copyright (c) 2015年 lllyyy. All rights reserved.
//

#import "newsImageListController.h"
#import "Header.h"
#import "ImageCell.h"
#import "DataService.h"
#import "PhotoViewController.h"
#import "BaseNavController.h"
@interface newsImageListController ()

@end

@implementation newsImageListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createCollectView];
    [self _loadData];
    self.view.backgroundColor = [UIColor redColor];
    // Do any additional setup after loading the view.
}


- (void)_createCollectView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((KWidth-50)/4, (KWidth-50)/4);
    _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    
}
- (void)_loadData{
    _imageModalArray = [[NSMutableArray alloc]init];
    NSArray *jsonArray = [DataService getJsonDataFromFile:@"image_list.json"];
    for (NSDictionary *dic in jsonArray) {
        ImageModal *modal = [[ImageModal alloc]init];
        [modal setValuesForKeysWithDictionary:dic];
        [_imageModalArray addObject:modal];
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _imageModalArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.modal = _imageModalArray[indexPath.row];
    return cell;
}


//功能抽离 代码不耦合

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    PhotoViewController *vc = [[PhotoViewController alloc]init];
    
    //当前点击的索引
    vc.currentIndex = indexPath.row;
    
    
    NSMutableArray *imageUrlArray = [[NSMutableArray alloc]init];
    
    for(int i = 0;i<_imageModalArray.count;i++){
        ImageModal *modal = _imageModalArray[i];
        NSString *urlStr = modal.image;
        [imageUrlArray addObject:urlStr];
    }

    vc.view.backgroundColor = [UIColor redColor];
    
    vc.imageUrlArray = imageUrlArray;
    
    BaseNavController *nav = [[BaseNavController alloc]initWithRootViewController:vc];
    
    
    [self presentViewController:nav animated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
